namespace CSharpPro._1016;

public class Demo
{
    public void Textprint1()
    {
        Console.WriteLine("请输入三个整数，用空格分隔： ");
        
        string[] input = Console.ReadLine().Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);

        if (input.Length != 3)
        {
            Console.WriteLine("输入格式不正确，请输入三个整数并用空格分隔");
            return;
        }

        try
        {
            int x = int.Parse(input[0]);
            int y = int.Parse(input[1]);
            int z = int.Parse(input[2]);

            int[] numbers = { x, y, z };

            Array.Sort(numbers);

            Console.WriteLine("从小到大的排序结果为： ");
            Console.WriteLine($"{numbers[0]} {numbers[1]} {numbers[2]}");
        }
        catch (FormatException)
        {
            Console.WriteLine("输入格式不正确，请确保输入的是整数");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"发生错误：{ex.Message}");
        }
    }

    public void Textprint2()
    {
        int sum = 0;
        Console.WriteLine("1-100之间所有不是7的倍数且不包含7的数字： ");

        for (int i = 0; i < 100; i++)
        {
            bool inMultpleOf7 = i % 7 == 0;
            
            bool contains = (i % 10 == 7) || (i / 10 == 7);

            if (!inMultpleOf7)
            {
                Console.Write(i + " ");
                sum += i;
            }
        }
    }

    public void Textprint3()
    {
        Console.WriteLine("九九乘法表： ");

        for (int i = 1; i <= 9; i++)
        {
            for (int j = 1; j <= i; j++)
            {
                Console.Write($"{j}*{i}={i * j}\t");
            }
            Console.WriteLine();
        }
    }

    public void Textprint4()
    {
        Console.WriteLine("1000以内的所有完数： ");

        for (int num = 2; num <= 1000; num++)
        {
            int sum = 0;

            for (int i = 1; i < num; i++)
            {
                if (num % i == 0)
                {
                    sum += i;
                }
            }

            if (sum == num)
            {
                Console.Write(num + " ");
            }
        }
        Console.WriteLine();
    }


    public void Textprint5()
    {
        Console.WriteLine("请输入一个正整数：");
        string input = Console.ReadLine();
    
        try
        {
            long number = long.Parse(input);
            
            if (number <= 0)
            {
                Console.WriteLine("请输入正整数！");
                return;
            }
            
            int digitCount = input.Length;
            
            Console.WriteLine($"这是一个{digitCount}位数");
            Console.Write("逆序打印各位数字：");
            
            char[] digits = input.ToCharArray();
            Array.Reverse(digits);
            foreach (char c in digits)
            {
                Console.Write(c + " ");
            }
            
            long temp = number;
            while (temp > 0)
            {
                Console.Write(temp % 10 + " ");
                temp /= 10;
            }
            
        
            Console.WriteLine();
        }
        catch (FormatException)
        {
            Console.WriteLine("输入格式不正确，请输入有效的正整数！");
        }
        catch (OverflowException)
        {
            Console.WriteLine("输入的数字过大，请输入较小的正整数！");
        }
    }

    public void Textprint6()
    {
        Console.WriteLine("请输入菱形的行数：");
        string input = Console.ReadLine();

        try
        {
            int rows = int.Parse(input);

            if (rows <= 0)
            {
                Console.WriteLine("请输入正整数");
                return;
            }

            int half = (rows + 1) / 2;

            for (int p = 1; p <= half; p++)
            {
                for (int i = 1; i <= half; i++)
                {
                    for (int j = half; j > i; j--)
                    {
                        Console.Write(" ");
                    }

                    for (int k = 1; k <= 2 * i - 1; k++)
                    {
                        Console.Write("*");
                    }

                    Console.WriteLine();
                }

                for (int i = half - 1; i >= 1; i--)
                {
                    for (int j = half; j > i; j--)
                    {
                        Console.Write(" ");
                    }

                    for (int k = 1; k <= 2 * i - 1; k++)
                    {
                        Console.Write("*");
                    }

                    Console.WriteLine();
                }
            }
        }
        catch (FormatException)
        {
            Console.WriteLine("输入格式不正确，请输入有效的整数");
        }
    }
    
}